package com.xiaowen.tool.threadUtil.ThreadExecutorManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: yf-wenhao
 * Date: 17/8/11
 * Time: 上午9:48
 */
public class SynThreadPoolExecutor extends java.util.concurrent.ThreadPoolExecutor {

    private static final Logger LOGGER = LoggerFactory.getLogger(SynThreadPoolExecutor.class);

    private final ThreadLocal<Long>  startTime =new ThreadLocal<>();

    public SynThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t,r);
        startTime.set(new Date().getTime());
        //LOGGER.info("thread : {} 开始执行－ ",t.getName());

    }

    protected void afterExecute(Runnable r, Throwable t) {
        Long time =new Date().getTime() - startTime.get();
        super.afterExecute(r,t);
        Thread thread =Thread.currentThread();
        //LOGGER.info("thread : {} 线程结束执行,运行时间为 : {} ",thread.getName(), time);

    }


}
